<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:33:05
 * @LastEditTime: 2022-08-25 21:33:51
 * @LastEditors: CHINAKAIYUAN
 * @Description: 
 */

namespace App\Models;

use App\Traits\CarryTrait;
use App\Traits\CarTrait;
use App\Traits\MaterialTrait;
use App\Traits\SoftDeletesTrait;
use App\Traits\StorageTrait;

class MarketDeliveryDetails extends BaseModel
    {
    use SoftDeletesTrait;
    use MaterialTrait;
    use StorageTrait;
    use CarryTrait;
    use CarTrait;
    public $timestamps = true;
    public $table = "sys_market_delivery_details";
    protected $guarded = [];
    public function status()
        {
        return $this->belongsTo(Dic::class, "status_id", "key")->where(function ($q)
            {
            $q->where("code", 'status_with_finance');
            });
        }
    //运输派车原地址
    public function getSourceAddr()
        {
        return $this->creator->department?->name ?? "极光";
        }
    public function order()
        {
        return $this->belongsTo(CustomerStorageDetail::class, "batchno", "batchno");
        }
    public function packs()
        {
        return $this->hasMany(MarketDeliveryDetailsPack::class, "delivery_id");
        }
    //运输派车目的地址
    public function getTargetAddr()
        {
        return $this->customer->name;
        }
    public function tax()
        {
        return $this->belongsTo(Dic::class, "tax_id", "key")->where(function ($q)
            {
            $q->where("code", 'FinanceTax');
            });
        }
    public function main()
        {
        return $this->belongsTo(MarketDelivery::class, "code", "code");
        }
    public function department()
        {
        return $this->belongsTo(Department::class);
        }

    public function customer()
        {
        return $this->belongsTo(Company::class, "main_customer_id");
        }
    public function _deleted($model)
        {
        $model->order?->updateQuantity();
        }
    public function _updated($model)
        {
        $model->order?->updateQuantity();
        }
    }
